﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using EntityInterfaces;

namespace DAL
{
    public class Lang : ILang
    {
        private lang entity;
        private shopnetEntities model;
        private Dictionary<String,String> trans_dict;

        public String Translate(String name) {
            return trans_dict[name];
        }
        public int id {
            get {
                return entity.langID;
            }
        }

        public Lang() {
            model = new shopnetEntities();
            trans_dict = new Dictionary<string, string>();
        }

        public void Save()
        {
            model.langs.AddObject(entity);
            try
            {
                model.SaveChanges();
            }
            catch (Exception exception)
            {
                // log
                throw new EntityException.LangException("Lang Save error", exception);
            }
        }
        public void Read(int id)
        {
            try
            {
                entity = model.langs.Single(s => s.langID == id);
            }
            catch (Exception exception)
            {
                //log
                throw new EntityException.LangException("Lang Read error", exception);
            }

            try
            {
                var list = model.translations.Where(s=> s.langID == id);
                foreach ( var i in list ){
                    trans_dict.Add(i.key, i.value);
                }
            }
            catch (Exception exception)
            {
                //log  
                throw new EntityException.LangException("Translation Read error", exception);
            }
        }
        public void Commit()
        {
            try
            {
                model.SaveChanges();
            }
            catch (Exception exception)
            {
                //log
                throw new EntityException.LangException("Lang Commit error", exception);
            }
        }
              
    }
}
